home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char rcsid[] = "$Header: movecycle.c,v 1.2 87/05/19 17:22:07 schoch Exp $";
- #endif
-
- #include "externs.h"
-
- #ifdef XKS
- movecycle()
- {
- fd_set readfds, fds;
- extern int sock;
- int n;
-
- XSelectInput(window, ExposeWindow | ExposeRegion |
- ButtonPressed | ButtonReleased | LeaveWindow | LeftDownMotion |
- KeyPressed);
- FD_ZERO(&fds);
- FD_SET(dpyno(), &fds);
- FD_SET(sock, &fds);
- for(;;) {
- while (XPending())
- handle_input();
- readfds = fds;
- n = select(FD_SETSIZE, &readfds, 0, 0, 0);
- if (n < 0) {
- if (errno == EINTR)
- continue;
- perror("select");
- exit(1);
- }
- if (n == 0)
- exit(0);
- if (FD_ISSET(dpyno(), &readfds))
- handle_input();
- if (FD_ISSET(sock, &readfds))
- if (handle_sock()) {
- FD_CLR(sock, &fds);
- close(sock);
- sock = -1;
- }
- }
-
- }
-
- extern bool note_on;
- int pawntries=0;
- char *colorname[3] = { "white", "black", "undecided" };
-
- /* This function is called when either side tries a move.
- * If the move is not valid, then we signal an error, no matter
- * who tries the move (this is so we can see what our opponent is
- * trying.
- * If the move is correct, then we toggle color.
- */
- movetry(from, to, whose)
- u_char whose;
- {
- /* We can cheat with this initialization of pawntries because
- * we know you can't take a pawn at the beginning of the games.
- */
- static pawnattempts=0;
- static LIST checkdirs = NIL;
- int l;
- LIST check ();
- char buf[128];
-
- mclear (CAPTURE);
-
- if ((l = legalmove (pawntries, &pawnattempts, checkdirs,
- from, to, color)) != TRUE
- && !drawok [color] && !drawok [1 - color]
- && !resign && !dead) {
- illegal (l, color);
- return l;
- }
- mclear (CHECK);
- mclear(LEGAL);
- if (!note_on) {
- if (ourcolor == WHITE)
- message("--- WHITE ---", MESSAGE);
- else
- message("--- BLACK ---", MESSAGE);
- }
-
- if (!drawok [1 - color] && !drawok [color]
- && !resign && !dead) {
- makemove (from, to, color);
- if (occupant [to] == KING && to == from + 2)
- makemove (from + 3, from + 1, color);
- if (occupant [to] == KING && to == from - 2)
- makemove (from - 4, from - 1, color);
- lastmovefrom = from;
- lastmoveto = to;
- }
- if (!resign && drawok [1 - color] && !drawok [color]) {
- drawok [1 - color] = FALSE;
- message("Draw refused.", LEGAL);
- }
-
- color = 1 - color;
-
- checkdirs = check (color);
-
- if (mate (pawnattempts, color)) {
- if (checkdirs != NIL) {
- message("CHECKMATE !", CHECK);
- sprintf(buf, "%s wins.", colorname[1 - color]);
- message(buf, TOMOVE);
- } else {
- message("STALEMATE", CHECK);
- mclear(TOMOVE);
- }
- state = OVER;
- mclear(PAWNTRIES);
- return 0;
- }
- if (insufficient () || (drawok [WHITE] && drawok [BLACK])) {
- message("Game ends in a draw.", CHECK);
- mclear(TOMOVE);
- mclear(PAWNTRIES);
- state = OVER;
- return 0;
- }
- if (resign) {
- sprintf(buf, "%s resigns.", colorname[whose]);
- message(buf, CHECK);
- mclear(TOMOVE);
- mclear(LEGAL);
- mclear(PAWNTRIES);
- state = OVER;
- return 0;
- }
- if (dead) {
- message("DEAD", CHECK);
- message("lost your opponent (sorry)", MESSAGE);
- state = OVER;
- return 0;
- }
-
- pawnattempts = 0;
- pawntries = countpawntries (color);
- if (drawok[1 - color]) {
- message("Draw offered.", LEGAL);
- if (color == ourcolor)
- message("Type y or n.", TOMOVE);
- else
- mclear(TOMOVE);
- } else {
- sprintf(buf, "%s to move", colorname[color]);
- message(buf, TOMOVE);
- }
-
- if (option [ANNOUNCEPAWNS]) {
- if(pawntries && pawnattempts < 3) {
- if (pawntries == 1)
- strcpy(buf, "1 pawntry");
- else
- sprintf(buf, "%d pawntries", pawntries);
- message(buf, PAWNTRIES);
- } else
- mclear(PAWNTRIES);
- } else
- mclear(PAWNTRIES);
-
- reportchecks (checkdirs, kingloc [color]);
- return 0;
- }
- #else XKS
- movecycle ()
- {
- int from, to, pawntries, pawnattempts, l;
- LIST check (), checkdirs;
-
- while (TRUE) {
- mclear(CLOCK);
- mclear(PROMPT);
- pawnattempts = 0;
- wprintw (win [TOMOVE], "%s to move\r", colorname [color]);
- pawntries = countpawntries (color);
- if (option [ANNOUNCEPAWNS] && pawntries && pawnattempts < 3)
- if (pawntries == 1)
- wprintw (win [PAWNTRIES], "1 pawntry");
- else
- wprintw (win [PAWNTRIES], "%d pawntries",
- pawntries);
- checkdirs = check (color);
- reportchecks (checkdirs, kingloc [color]);
- if (mate (pawnattempts, color)) {
- mclear(CHECK);
- if (checkdirs != NIL)
- message("CHECKMATE !", CHECK);
- else
- message("STALEMATE", CHECK);
- state = OVER;
- break;
- }
- if (insufficient () || (drawok [WHITE] && drawok [BLACK])) {
- mclear(CHECK);
- message("DRAW", CHECK);
- state = OVER;
- break;
- }
- if (resign) {
- mclear(CHECK);
- message("RESIGNS", CHECK);
- state = OVER;
- break;
- }
- if (dead) {
- mclear(CHECK);
- message("DEAD", CHECK);
- mclear(MESSAGE);
- state = OVER;
- waddstr (win[MESSAGE], "lost your\nopponent\n(sorry)");
- }
- entermove (&from, &to, color, pawntries);
- while ((l = legalmove (pawntries, &pawnattempts, checkdirs,
- from, to, color)) != TRUE
- && !drawok [color] && !drawok [1 - color]
- && !resign && !dead) {
- illegal (l, color);
- entermove (&from, &to, color, pawntries);
- }
- mclear(CAPTURE);
- mclear(PAWNTRIES);
- mclear(CHECK);
- if (!drawok [1 - color] && !drawok [color]
- && !resign && !dead) {
- makemove (from, to, color);
- if (occupant [to] == KING && to == from + 2)
- makemove (from + 3, from + 1, color);
- if (occupant [to] == KING && to == from - 2)
- makemove (from - 4, from - 1, color);
- lastmovefrom = from;
- lastmoveto = to;
- }
- if (drawok [1 - color] && !drawok [color])
- drawok [color] = FALSE;
- color = 1 - color;
- mclear(MESSAGE);
- }
- mclear(TOMOVE);
- mclear(CLOCK);
- mclear(CAPTURE);
- mclear(PAWNTRIES);
- while (TRUE) {
- waddstr (win [PROMPT], "\rreview game?");
- mclear(MESSAGE);
- waddstr (win [MESSAGE], "type y or n");
- mclear(INPUT);
- waddstr (win [INPUT], ": ");
- move (win [INPUT]->_cury + win [INPUT]->_begy,
- win [INPUT]->_curx + win [INPUT]->_begx);
- refresh ();
- mclear(CHECK);
- switch (getchar ()) {
- case 'y':
- mclear(PROMPT);
- mclear(MESSAGE);
- review();
- break;
- case 'n':
- error ((char *) NULL);
- default:
- printf ("\007");
- }
- }
- }
- #endif XKS
-